﻿
Public Class backgroundDownload
    Public Const functionName As String = "bgdl"

#If RETIRED Then
    Private Shared Function imageVar(ByVal functionName As String) As String
        Return functionName + "i"
    End Function
#End If

    Public Shared Sub register(ByVal inlineScript As inlineScript, ByVal functionName As String)
        assert(Not inlineScript Is Nothing)
        assert(strsame(document.name(inlineScript), "d"))
#If RETIRED Then
        Dim imgVar As String = Nothing
        imgVar = imageVar(functionName)
        var.invoke(inlineScript, imgVar, Nothing)
        inlineScript.addscript(functionName, _
                               "function " + functionName + "(u){" + _
                                   "if(d.images&&!" + imgVar + ")" + imgVar + "=new Image;" + _
                                   "if(" + imgVar + ")" + imgVar + ".src=u;}")
#End If
        inlineScript.addscript(functionName, _
                       "function " + functionName + "(u){" + _
                           "if(d.images){var i=new Image;i.src=u;}}")
    End Sub

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        register(inlineScript, functionName)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal s As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        Return invoke(inlineScript, functionName, s, actionTarget)
    End Function

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal functionName As String, _
                                  ByVal s As String, ByRef actionTarget As String) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript, functionName)
        End If
        actionTarget += functionName + "(" + s + ")"
        Return actionTarget
    End Function
End Class
